﻿#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //可以用该接口设置QMainWindow 的中心区域所占用的组件
    setCentralWidget(ui->centralwidget);
    //设置部件右键菜单
    ui->m_listWidget->setContextMenuPolicy(Qt::CustomContextMenu);
    //为相关的toolbutton设置action
    setActionForToolButton();
    //创建相关选择下来菜单
    createSelectActionMenu();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::setActionForToolButton()
{
    //toolbutton通过设置action以及显示方式，设置这里的显示
    ui->m_tbn_linit->setDefaultAction(ui->m_action_linit);
    ui->m_tbn_ladd->setDefaultAction(ui->m_action_ladd);
    ui->m_tbn_ldel->setDefaultAction(ui->m_action_ldel);
    ui->m_tbn_linsert->setDefaultAction(ui->m_action_linsert);
    ui->m_tbn_ldelone->setDefaultAction(ui->m_action_ldelone);

    ui->m_tbn_SelectAll->setDefaultAction(ui->m_action_lselectall);
    ui->m_tbn_SelectNull->setDefaultAction(ui->m_action_lselectNone);
    ui->m_tbn_SelectInvs->setDefaultAction(ui->m_action_lselectlnvs);
}

void MainWindow::createSelectActionMenu()
{
    QMenu* menuSelection = new QMenu(this);
    menuSelection->addAction(ui->m_action_lselectall);
    menuSelection->addAction(ui->m_action_lselectNone);
    menuSelection->addAction(ui->m_action_lselectlnvs);

    //下拉小箭头 设置显示方式，关联action,设置menu
    ui->m_tbn_SelectItem->setPopupMode(QToolButton::MenuButtonPopup);
    ui->m_tbn_SelectItem->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
    ui->m_tbn_SelectItem->setDefaultAction(ui->m_action_lselectPopMenu);


    //在菜单工具栏上新增一个控件 设置下拉，设置显示，关联action，设置menu，加入工具栏
    QToolButton *tbn = new QToolButton(this);
    tbn->setPopupMode(QToolButton::InstantPopup); //下拉菜单
    tbn->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
    tbn->setDefaultAction(ui->m_action_lselectPopMenu);

    ui->m_tbn_SelectItem->setMenu(menuSelection);
    tbn->setMenu(menuSelection);

    ui->m_main_toolBar->addWidget(tbn);
    ui->m_main_toolBar->addSeparator();
    //在设置action的时候，要做必要的信号槽关联
    ui->m_main_toolBar->addAction(ui->m_action_quit);
    //对下拉选项按钮本身做关联点击事件
    connect(ui->m_action_lselectPopMenu, SIGNAL(trigered()), ui->m_action_lselectlnvs, SLOT(trigger));
//    connect(comboFontName,&QFontComboBox::currentFontChanged,this, &MainWindow::do_fontSelected);
}

//主要练习qlistwidget控件的相关使用
//先初步熟悉吧，再优化。
//主要是action的设置 以及qtoolbutton的相关关联和设置学习 以及右键选项
void MainWindow::on_m_isCheck_clicked(bool checked)
{
    //获取每一个项  并遍历进行设置
    int count = ui->m_listWidget->count();
    QListWidgetItem *aItem;
    for(int i=0; i<count; i++)
    {
        aItem = ui->m_listWidget->item(i);
        if(checked)
            aItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEditable |Qt::ItemIsUserCheckable |Qt::ItemIsEnabled);
        else
            aItem->setFlags(Qt::ItemIsSelectable |Qt::ItemIsUserCheckable |Qt::ItemIsEnabled);
    }
}

//对应的list中右键菜单
void MainWindow::on_m_listWidget_customContextMenuRequested(const QPoint &pos)
{
    Q_UNUSED(pos);

    QMenu *menu = new QMenu(this);
    menu->addAction(ui->m_action_linit);
    menu->addAction(ui->m_action_ldel);
    menu->addAction(ui->m_action_ladd);
    menu->addAction(ui->m_action_linsert);
    menu->addAction(ui->m_action_ldelone);
    menu->addSeparator();
    menu->addAction(ui->m_action_lselectall);
    menu->addAction(ui->m_action_lselectNone);
    menu->addAction(ui->m_action_lselectlnvs);

    menu->exec(QCursor::pos()); //在鼠标光标位置处进行显示
    delete menu;
}

//这里对列表的初始化
void MainWindow::on_m_action_linit_triggered()
{
    QIcon aIcon;
    aIcon.addFile(":/images/icons/check2.ico");

    QListWidgetItem *aItem;

    ui->m_listWidget->clear();
    bool chk = ui->m_isCheck->isChecked();

    for(int i=0; i<10; i++)
    {
        aItem = new QListWidgetItem();
        aItem->setText(QString("测试 %1").arg(i)); //设置文字
        aItem->setIcon(aIcon);                    //设置图标
        aItem->setCheckState(Qt::Checked);        //设置选中，在前面增加了选择框

        if(chk) //设置项是否可以编辑
            aItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEditable |Qt::ItemIsUserCheckable |Qt::ItemIsEnabled);
        else
            aItem->setFlags(Qt::ItemIsSelectable |Qt::ItemIsUserCheckable |Qt::ItemIsEnabled);

        ui->m_listWidget->addItem(aItem);
    }
}

void MainWindow::on_m_action_linsert_triggered()
{
    //获取当前行
    int cur_row = ui->m_listWidget->currentRow();

    QListWidgetItem *aItem = new QListWidgetItem("插入测试");
    aItem->setIcon(QIcon(":/images/icons/check2.ico"));
    aItem->setCheckState(Qt::Checked);
    if(ui->m_isCheck)
        aItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEditable |Qt::ItemIsUserCheckable |Qt::ItemIsEnabled);
    else
        aItem->setFlags(Qt::ItemIsSelectable |Qt::ItemIsUserCheckable |Qt::ItemIsEnabled);

    ui->m_listWidget->insertItem(cur_row, aItem);
}

void MainWindow::on_m_action_ladd_triggered()
{
    QListWidgetItem *aItem = new QListWidgetItem("添加测试");
    aItem->setIcon(QIcon(":/images/icons/check2.ico"));
    aItem->setCheckState(Qt::Checked);
    if(ui->m_isCheck)
        aItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEditable |Qt::ItemIsUserCheckable |Qt::ItemIsEnabled);
    else
        aItem->setFlags(Qt::ItemIsSelectable |Qt::ItemIsUserCheckable |Qt::ItemIsEnabled);

    ui->m_listWidget->addItem(aItem);
}

void MainWindow::on_m_action_ldel_triggered()
{
    ui->m_listWidget->clear();
}

void MainWindow::on_m_action_ldelone_triggered()
{
    int cur_row = ui->m_listWidget->currentRow();
    QListWidgetItem *aItem = ui->m_listWidget->takeItem(cur_row);
    delete aItem;
}

void MainWindow::on_m_action_lselectall_triggered()
{
    int count = ui->m_listWidget->count();
    for(int i=0;i<count; i++)
    {
        QListWidgetItem *aItem = ui->m_listWidget->item(i);
        aItem->setCheckState(Qt::Checked);
    }
}

void MainWindow::on_m_action_lselectNone_triggered()
{
    int count = ui->m_listWidget->count();
    for(int i=0;i<count; i++)
    {
        QListWidgetItem *aItem = ui->m_listWidget->item(i);
        aItem->setCheckState(Qt::Unchecked);
    }
}

void MainWindow::on_m_action_lselectlnvs_triggered()
{
    int count = ui->m_listWidget->count();
    QListWidgetItem *aItem =nullptr;
    for(int i=0;i<count; i++)
    {
        aItem = ui->m_listWidget->item(i);
        if(aItem->checkState() == Qt::Checked)
            aItem->setCheckState(Qt::Unchecked);
        else
            aItem->setCheckState(Qt::Checked);
    }
}

void MainWindow::on_m_tbn_SortAsc_clicked()
{
    ui->m_listWidget->sortItems(Qt::AscendingOrder);
}

void MainWindow::on_m_tbn_SortDes_clicked()
{
    ui->m_listWidget->sortItems(Qt::DescendingOrder);
}

void MainWindow::on_m_isSort_clicked(bool checked)
{
    ui->m_listWidget->setSortingEnabled(checked);
}

//这里只是演示一些关于点击listWidget项事件时的触发

//当前项发生变化 选中的item项发生变化
void MainWindow::on_m_listWidget_currentItemChanged(QListWidgetItem *current, QListWidgetItem *previous)
{
    QString str;

    if(current != nullptr) //必须增加 否则重新初始化后，会有问题
    {
        if(previous == NULL)
            str = "当前项："+current->text();
        if(previous != NULL)
            str = "先前项："+current->text()+"; 当前项："+previous->text();
        ui->m_lineEdit->setText(str);
    }

    ui->m_pte_display->appendPlainText("currentItemChanged()信号被发射");
}

//双击时的触发
void MainWindow::on_m_listWidget_itemDoubleClicked(QListWidgetItem *item)
{
    ui->m_pte_display->appendPlainText("itemDoubleClicked()信号被发射,当前项内容："+item->text());
}

void MainWindow::on_m_listWidget_currentRowChanged(int currentRow)
{
    ui->m_pte_display->appendPlainText(QString("currentRowChanged()信号被发射,当前行：%1").arg(currentRow));
}
//选中即触发
void MainWindow::on_m_listWidget_currentTextChanged(const QString &currentText)
{
    ui->m_pte_display->appendPlainText(QString("currentTextChanged()信号被发射,内容为：%1").arg(currentText));
}

//键盘上enter触发时
void MainWindow::on_m_listWidget_itemActivated(QListWidgetItem *item)
{
    ui->m_pte_display->appendPlainText(QString("itemActivated()信号被发射,内容为：%1").arg(item->text()));
}

//项中内容发生变化触发
void MainWindow::on_m_listWidget_itemChanged(QListWidgetItem *item)
{
    ui->m_pte_display->appendPlainText(QString("itemChanged()信号被发射,内容为：%1").arg(item->text()));
}

//鼠标进入某项时触发
void MainWindow::on_m_listWidget_itemEntered(QListWidgetItem *item)
{
    ui->m_pte_display->appendPlainText(QString("itemEntered()信号被发射,内容为：%1").arg(item->text()));
}

//按下时触发
void MainWindow::on_m_listWidget_itemPressed(QListWidgetItem *item)
{
    ui->m_pte_display->appendPlainText(QString("itemPressed()按下信号被发射,内容为：%1").arg(item->text()));
}

//选中状态发生变化时触发
void MainWindow::on_m_listWidget_itemSelectionChanged()
{
    ui->m_pte_display->appendPlainText(QString("itemSelectionChanged()选中信号被发射"));
}

//某项被单击时触发
void MainWindow::on_m_listWidget_itemClicked(QListWidgetItem *item)
{
    ui->m_pte_display->appendPlainText(QString("itemClicked()单击信号被发射,内容为：%1").arg(item->text()));
}
